<template>
  <el-dialog
    title="修改公告"
    width="1200px"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :modal-append-to-body="false"
  >
    <el-form :model="form" :rules="rules" ref="form">
      <div class="dialog-box">
        <div class="dialog-box-item">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" autocomplete="off" clearable></el-input>
          </el-form-item>
<!--          <div class="el-form-item__label" style="width: 100%;text-align: left;"><span class="required">*</span>预览图</div>-->
<!--          <my-upload-->
<!--            :limit="1"-->
<!--            :preview="previewPic"-->
<!--            @upload="uploadShow"-->
<!--            @remove="removeUploadShow"-->
<!--          />-->
        </div>
        <div class="dialog-box-item">
          <el-divider content-position="left">公告内容</el-divider>
          <el-input type="textarea" v-model="form.content" :rows="5" autocomplete="off" clearable></el-input>
          <!--<d2-quill-editor v-model="form.content"></d2-quill-editor>-->
        </div>
      </div>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  // import MyUpload from '@/components/my-upload'
  import {htmlEncode, htmlDecode} from '@/libs/util.htmlcode'
  import { mapActions } from 'vuex'
  export default {
    name: 'page',
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      info: {
        type: Object,
        default: null
      }
    },
    data () {
      return {
        form: {
          id: '',
          title: '',
          content: '',
          locationType: 0,
          moduleType: 0,
          photoUrl: '',
          photoOssId: ''
        },
        previewPic: [],
        rules: {
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' },
            { min: 1, max: 100, message: '最多 100 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    // components: {
    //   MyUpload
    // },
    mounted () {
      this.$nextTick(() => {
        this.getInfo()
      })
    },
    methods: {
      ...mapActions('d2admin/pm.ad', [
        'adUpdate',
        'adInfo'
      ]),
      // 详情
      getInfo () {
        this.adInfo({
          id: this.info.id
        }).then(res => {
          res.locationType = String(res.locationType)
          res.content = htmlDecode(res.content)
          this.form = res
          this.previewPic = []
          this.previewPic.push(res.photoUrl)
        })
      },
      // 取消
      cancel() {
        this.$emit('cancel', false)

      },
      // 提交回调
      submit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            // if (!this.form.photoUrl) {
            //   this.$message.error('请上传预览图')
            //   return false
            // }
            this.form.content = htmlEncode(this.form.content)
            this.submitHandle(this.form)
          }
        })
      },
      // 提交回调
      submitHandle(form) {
        this.adUpdate(form).then(res => {
          this.$emit('submit', false)
        })
      },
      // 上传展示图片
      uploadShow(data) {
        this.form.photoOssId = data[0].ossId
        this.form.photoUrl = data[0].url
      },
      // 删除展示图片
      removeUploadShow(data) {
        this.form.photoOssId = ''
        this.form.photoUrl = ''
      }
    }
  }
</script>

<style lang="scss" scoped>
  .dialog-box {
    display: flex;
    flex-direction: row;

    .dialog-box-item {
      flex: 1;
      height: 500px;
      padding: 10px;
      &:first-child {
        width: 300px;
        flex: none;
      }
    }
    .required {
      color: #F56C6C;
      margin-right: 4px;
    }
  }
</style>
